import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class PddScrap():
    def setup_method(self, method):
        # 绑定指定端口
        options = webdriver.ChromeOptions()
        options.add_experimental_option("debuggerAddress", "127.0.0.1:8888")
        self.driver = webdriver.Chrome(options=options)
        self.vars = {}
        method()

    def teardown_method(self, method):
        self.driver.quit()

    def query_to_login(self):
        self.driver.get("https://mms.pinduoduo.com/login/?redirectUrl=https%3A%2F%2Fmms.pinduoduo.com%2F")
        self.driver.set_window_size(1452, 884)
        WebDriverWait(self.driver, 10).until(
            expected_conditions.presence_of_element_located((By.XPATH, "//div[@class=\"tab-item last-item\"]")))
        self.driver.find_element(By.XPATH, "//div[@class=\"tab-item last-item\"]").click()
        self.driver.find_element(By.ID, "usernameId").click()
        self.driver.find_element(By.ID, "usernameId").send_keys("##########")
        self.driver.find_element(By.ID, "passwordId").click()
        self.driver.find_element(By.ID, "passwordId").send_keys("##########")
        self.driver.find_element(By.XPATH, "//button[contains(.,\'登录\')]").click()
        print(self.driver.current_url)
        open('mms.pinduoduo.com.html', 'w').write(self.driver.page_source)


if __name__ == '__main__':
    pdd_scrap = PddScrap()
    pdd_scrap.setup_method(pdd_scrap.query_to_login)
